/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.entities;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.VehicleStatus;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author Tomas Marton
 */

@Entity
public class Vehicle implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="vehicle_id")
    private Long id;
    
    @Column(name = "VehicleRe_gistration_Plate", length = 10, nullable = false)
    @NotNull
    @Size(max=10,message="StateLicencePlate must not have size greater than 10.")
    private String stateLicencePlate;
    
    @Column(name = "Car_Producer", nullable = false)
    @NotNull(message="carProducer has to be properly set.Can't be null.")
    private String carProducer;
    
    @Column(name = "Car_Model",nullable=false)
    @NotNull(message="CarModel has to be properly set.Can't be null.")
    private String carModel;
    
    @Column(name = "Year_Of_Manufacture")
    @Min(1990)
    private int yearOfManufacture;
    
    @Column(name = "Engine_Type", length = 10, nullable = false)
    @NotNull(message="EngineType has to be properly set.Can't be null.")
    @Size(max=10,message="EngineType must not have size greater than 10")
    private String engineType;
    
    @Column(name = "Vin", length = 17, nullable = false)
    @Size(max=17,min=17,message="Vin number must be 17 digits long.")
    @NotNull
    private String vin;
    
    @Column(name = "Number_Of_Kms")
    @Min(0)
    private int numberOfKms;
    
    @Column(name ="Position",nullable=false)
    @NotNull(message="AvailablePositions have to be properly set.Can't be null.")       
    private EmployeePosition availableForPositions;
    
    @OneToMany(fetch = FetchType.EAGER,
    orphanRemoval = true,
    cascade= {CascadeType.MERGE}
    )  
    @JoinTable(name = "VEHICLE_SERVICECHECKS", joinColumns = { @JoinColumn(name = "vehicle_id") },
               inverseJoinColumns = { @JoinColumn(name = "service_check_id") })
    @NotNull
    private Set<ServiceCheck> controlServiceCheck;
    
    @Column(name = "Status",nullable=false)
    @NotNull(message="Status has to be properly set.Can't be null.")
    private VehicleStatus status;
    
    public Vehicle(){
        this.controlServiceCheck = new HashSet<ServiceCheck>();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStateLicencePlate() {
        return stateLicencePlate;
    }

    public void setStateLicencePlate(String stateLicencePlate) {
        this.stateLicencePlate = stateLicencePlate;
    }

    public String getCarProducer() {
        return carProducer;
    }

    public void setCarProducer(String carProducer) {
        this.carProducer = carProducer;
    }

    public String getCarModel() {
        return carModel;
    }

    public void setCarModel(String carModel) {
        this.carModel = carModel;
    }   

    public String getEngineType() {
        return engineType;
    }

    public void setEngineType(String engineType) {
        this.engineType = engineType;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }  
    
    public VehicleStatus getStatus() {
        return status;
    }

    public void setStatus(VehicleStatus status) {
        this.status = status;
    }

    public int getYearOfManufacture() {
        return yearOfManufacture;
    }

    public void setYearOfManufacture(int yearOfManufacture) {
        this.yearOfManufacture = yearOfManufacture;
    }
    
    public Set<ServiceCheck> getControlServiceCheck() {
        return this.controlServiceCheck;
    } 
    
    public void setControlServiceCheck(Set<ServiceCheck> serviceCheck) {
        // set last Checks to current number of kms
        if(serviceCheck != null){
        for(ServiceCheck s : serviceCheck){
            s.setLastCheck(numberOfKms);
        }
        }
        this.controlServiceCheck = serviceCheck;
    }
    
    public void addControlServiceCheck(ServiceCheck serviceCheck) {   
        serviceCheck.setLastCheck(numberOfKms);
        this.controlServiceCheck.add(serviceCheck);
    }  
    
    public void disconnectServiceCheck(ServiceCheck serviceCheck){
        if(serviceCheck == null || serviceCheck.getId() == null){
            throw new IllegalArgumentException("ServiceCheck is null or not in database");
        }
        this.controlServiceCheck.remove(serviceCheck);
    }
    
    public Set<ServiceCheck> disconnectAllServiceChecks(){
        Set<ServiceCheck> result = new HashSet<ServiceCheck>(controlServiceCheck);
        this.controlServiceCheck.clear();
        return result;
    }
    public void setNumberOfKms(int numberOfKms) {
        this.numberOfKms = numberOfKms;
    }

    public EmployeePosition getAvailableForPositions() {
        return availableForPositions;
    }

    public void setAvailableForPositions(EmployeePosition availableForPositions) {
        this.availableForPositions = availableForPositions;
    }

    public int getNumberOfKms() {
        return numberOfKms;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Vehicle)) {
            return false;
        }
        Vehicle other = (Vehicle) object;
        if ((this.id != null && !this.id.equals(other.id)) || (this.id == null && other.id != null)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Vehicle{" + "id=" + id + ", stateLicencePlate=" + stateLicencePlate + ", carProducer=" + carProducer + ", carModel=" + carModel + ", yearOfManufacture=" + yearOfManufacture + ", engineType=" + engineType + ", vin=" + vin + ", numberOfKms=" + numberOfKms + ", availableForPositions=" + availableForPositions + ", controlServiceCheck=" + controlServiceCheck + ", status=" + status + '}';
    }

    
}
